package cheme.ProcessEditor.Parts.Streams;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.ConnectionLocator;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;
import org.eclipse.gef.handles.ConnectionHandle;
import org.eclipse.gef.tools.ConnectionEndpointTracker;


public class ChemeStreamEndpointPolicy extends ConnectionEndpointEditPolicy
{
	private int handlesize = 10;
	
	@Override
	public Command getCommand(Request request)
	{
		//System.out.println("ChemeStreamEndpointPolicy " + request.getType());
		
		return super.getCommand(request);
	}
	
	abstract class ChemeConnectionHandle extends ConnectionHandle
	{
		{
			setPreferredSize(new Dimension(handlesize, handlesize));
		}
		
		public ChemeConnectionHandle()
		{
			super();
		}

		public ChemeConnectionHandle(boolean fixed)
		{
			super(fixed);
		}

//		public void paintFigure(Graphics graphics)
//		{
//			graphics.pushState();
//			graphics.setAntialias(SWT.ON);
//			super.paintFigure(graphics);
//			graphics.popState();
//		}
	}
	
	public class ChemeConnectionEndHandle extends ChemeConnectionHandle
	{
		public ChemeConnectionEndHandle(ConnectionEditPart owner)
		{
			setOwner(owner);
			setLocator(new ConnectionLocator(getConnection(), ConnectionLocator.TARGET));
		}

		public ChemeConnectionEndHandle(ConnectionEditPart owner, boolean fixed)
		{
			super(fixed);
			setOwner(owner);
			setLocator(new ConnectionLocator(getConnection(), ConnectionLocator.TARGET));
		}

		public ChemeConnectionEndHandle()
		{
		}

		protected DragTracker createDragTracker()
		{
			if (isFixed())
			{
				return null;
			}
			
			ConnectionEndpointTracker tracker;
			tracker = new ConnectionEndpointTracker((ConnectionEditPart)getOwner());
			tracker.setCommandName(RequestConstants.REQ_RECONNECT_TARGET);
			tracker.setDefaultCursor(getCursor());
			
			return tracker;
		}
	}
	
	public class ChemeConnectionStartHandle extends ChemeConnectionHandle
	{
		public ChemeConnectionStartHandle(ConnectionEditPart owner)
		{
			setOwner(owner);
			setLocator(new ConnectionLocator(getConnection(), ConnectionLocator.SOURCE));
		}

		public ChemeConnectionStartHandle(ConnectionEditPart owner, boolean fixed)
		{
			super(fixed);
			setOwner(owner);
			setLocator(new ConnectionLocator(getConnection(), ConnectionLocator.SOURCE));
		}

		public ChemeConnectionStartHandle()
		{
		}

		protected DragTracker createDragTracker()
		{
			if (isFixed()) 
			{
				return null;
			}
			
			ConnectionEndpointTracker tracker;
			tracker = new ConnectionEndpointTracker((ConnectionEditPart)getOwner());
			tracker.setCommandName(RequestConstants.REQ_RECONNECT_SOURCE);
			tracker.setDefaultCursor(getCursor());
			
			return tracker;
		}
	}
	
	@Override
	protected List createSelectionHandles()
	{
		List list = new ArrayList();
		list.add(new ChemeConnectionEndHandle((ConnectionEditPart)getHost()));
		list.add(new ChemeConnectionStartHandle((ConnectionEditPart)getHost()));
	 	return list;
	}
}
